<template>
  <div>
    <el-card class="ivu-mt">
      <el-form :model="addForm" :rules="rulesForm" ref="refForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="网站logo" prop="img" ref="imageImgRef">
          <el-upload
            ref="imgRef"
            :action="upload.url"
            :headers="upload.headers"
            :accept="upload.accept"
            list-type="picture-card"
            :data="upload.data"
            :limit="upload.limitCount"
            :file-list="filePath"
            :on-remove="handleRemove"
            :on-exceed="onExceed"
            :on-success="onSuccess"
            :on-preview="handlePictureCardPreview">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="网站名称" prop="web_site_title">
          <el-input v-model="addForm.web_site_title" placeholder="设置网站名称"></el-input>
        </el-form-item>
        <el-form-item label="网站邮箱" prop="web_site_email">
          <el-input v-model="addForm.web_site_email" placeholder="设置网站邮箱"></el-input>
        </el-form-item>
        <el-form-item label="网站地址" prop="web_site_address">
          <el-input v-model="addForm.web_site_address" placeholder="设置网站内部地址"></el-input>
        </el-form-item>
        <el-form-item label="服务热线" prop="web_site_hotline">
          <el-input v-model="addForm.web_site_hotline" placeholder="全国服务热线,如“  0551-63682755"></el-input>
        </el-form-item>
        <el-form-item label="服务时间段" prop="web_site_time_slot">
          <el-input v-model="addForm.web_site_time_slot" placeholder="全国服务热线时间段,如“ 09：00-17:00"></el-input>
        </el-form-item>
        <el-form-item label="网站备案号" prop="web_site_icp">
          <el-input v-model="addForm.web_site_icp" placeholder="设置在网站底部显示的备案号，如“ 皖ICP备13010454号-2"></el-input>
        </el-form-item>
        <el-form-item label="版权信息" prop="web_site_copy">
          <el-input v-model="addForm.web_site_copy" placeholder="设置在网站底部显示的版权信息"></el-input>
        </el-form-item>
        <div v-for="(item, index) in addForm.moreNotifyObject" :key="item.key">
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="友情链接">
                <el-input v-model="item.title" placeholder="设置在网站底部的友情链接"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="url">
                <el-input v-model="item.url" placeholder="设置友情链接的url"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-button @click="delCul(item)">删除</el-button>
            </el-col>
          </el-row>
        </div>
        <el-form-item>
          <el-button type="primary" size="small" @click="addDomain">添加友链</el-button>
          <el-button type="primary" size="small"  @click="submitForm('refForm')">提交</el-button>
          <el-button size="small"  @click="resetForm('refForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "index",
  data(){
    let checkImg = (rule, value, callback) => {
      if (!this.hasFmt) { //hasFmt设置为true/false
        callback(new Error("请上传Logo图片"));
      } else {
        callback();
      }
    }
    return{
      dialogVisible: false,
      dialogImageUrl:'',
      hasFmt:false,
      system_id:1,
      addForm:{
        img:[],
        web_site_title:'',
        web_site_email:'',
        web_site_address:'',
        web_site_icp:'',
        web_site_copy:'',
        web_site_time_slot:'',
        web_site_hotline:'',
        moreNotifyObject: [
          {
            title: '',
            url: ''
          }
        ]
      },
      upload: {
        url: this.$action_url,
        headers: {Authorization: window.sessionStorage.getItem('token')},
        data:{
          folder:'logo_img'
        },
        name: 'file',
        accept: 'image/*',
        limitCount: 1,
      },
      rulesForm:{
        img: [{ required: true, validator: checkImg }],
        web_site_title: [
          { required: true, message: '请输入网站名称', trigger: 'blur' },
        ],
        web_site_address: [
          { required: true, message: '请输入网站地址', trigger: 'blur' },
        ],
        web_site_icp: [
          { required: true, message: '请输入网站备案号', trigger: 'blur' }
        ],
        web_site_copy: [
          { required: true, message: '请输入版权信息', trigger: 'blur' }
        ],
        web_site_time_slot: [
          { required: true, message: '请输入服务热线时间段', trigger: 'blur' }
        ],
        web_site_hotline: [
          { required: true, message: '请输入全国服务热线', trigger: 'blur' }
        ],
      },
      filePath:[],
    }
  },
  created() {
    this.getSystemList();

  },
  methods:{
    async getSystemList(){
      const {data: res} = await this.$http.get('dashboard/system_list',{params:{
        id:this.system_id }})
      if (res.code !== 200) return this.$message.error(res.data.message)
      this.addForm = res.data.content
      if (res.data.content.img.length > 0){
        this.hasFmt = true
      }
      this.filePath = res.data.content.img
    },
    submitForm(formName){
      this.$refs[formName].validate(async valid => {
        if (!valid) return this.$message.error('输入不合法');
        const {data: result} = await this.$http.post('dashboard/save_system', this.addForm)
        if (result.code !== 200) return this.$message.error(result.data.message)
        this.$message.success('更新成功')
        this.getSystemList()
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleRemove(file){
      const i = this.addForm.img.findIndex(x => x.image === file)
      //通过数组的splice方法 把图片从img_path 数组中删除
      this.addForm.img.splice(i, 1)
      if(this.addForm.img.length === 0){
        this.hasFmt = false;
      }
      this.$refs.imageImgRef.validate();
    },
    onExceed(files, fileList){
      if(files.length === 1){
        return this.$message.error('图片超出1张图片的限制,请删除后重新上传')
      }
    },
    onSuccess(response, file, fileList){
      if (response.code !== 200) return this.$message.error(response.data.message)
      this.hasFmt = true;
      const path = {url:this.$http_url + 'storage/' + response.data.list.path,uid:file.uid,name:file.name}
      this.addForm.img.push(path)
      this.$refs.imageImgRef.clearValidate();
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    delCul(item) {
      let index = this.addForm.moreNotifyObject.indexOf(item)
      if (index !== -1) {
        this.addForm.moreNotifyObject.splice(index, 1)
      }
    },
    addDomain() {
      this.addForm.moreNotifyObject.push({
        title: '',
        url: ''
      })
    }
  }
}
</script>

<style scoped>

</style>
